package com.lhh.springcommon.config;

import com.lhh.springcommon.config.JwtConfigurationProperties;
import com.lhh.springcommon.config.RequestConfigurationProperties;
import com.lhh.springcommon.interceptor.JwtInterceptor;
import com.lhh.springcommon.interceptor.RequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置拦截器
 * @author 李厚航
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    private final JwtInterceptor jwtInterceptor;

    private final RequestInterceptor requestInterceptor;

    private final JwtConfigurationProperties jwtConfigurationProperties;

    private final RequestConfigurationProperties requestConfigurationProperties;

    @Autowired
    public WebConfiguration(JwtInterceptor jwtInterceptor,
                            RequestInterceptor requestInterceptor,
                            JwtConfigurationProperties jwtConfigurationProperties,
                            RequestConfigurationProperties requestConfigurationProperties) {
        this.jwtInterceptor = jwtInterceptor;
        this.requestInterceptor = requestInterceptor;
        this.jwtConfigurationProperties = jwtConfigurationProperties;
        this.requestConfigurationProperties = requestConfigurationProperties;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestInterceptor).excludePathPatterns(requestConfigurationProperties.getExcluded().getPath());
        registry.addInterceptor(jwtInterceptor).excludePathPatterns(jwtConfigurationProperties.getExcluded().getPath());
    }
}
